home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / FocusManager.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.0 KB  |  130 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)FocusManager.java    1.9 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.util.Hashtable;
  17. import java.awt.event.KeyEvent;
  18. import java.awt.Component;
  19.  
  20. /**
  21.  * Swing Focus Manager
  22.  *
  23.  * @version 1.9 08/26/98
  24.  * @author Arnaud Weber
  25.  */
  26. public abstract class FocusManager {
  27.  
  28.     /** This property name is used to get the FocusManager implementation
  29.      *  that should be used for a given UI
  30.      */
  31.     public static final String FOCUS_MANAGER_CLASS_PROPERTY = 
  32.         "FocusManagerClassName";
  33.  
  34.     private static final Object focusManagerKey = FocusManager.class;
  35.  
  36.     /** Return the FocusManager for the calling thread 
  37.      *  There is one FocusManager per thread group
  38.      */
  39.     public static FocusManager getCurrentManager() {
  40.         FocusManager result = 
  41.             (FocusManager)SwingUtilities.appContextGet(focusManagerKey);
  42.         if(result == null) {
  43.             String className = 
  44.                 UIManager.getString(FOCUS_MANAGER_CLASS_PROPERTY);
  45.             try {
  46.                 Class c = Class.forName(className);
  47.                 if(c != null) {
  48.                     result = (FocusManager) c.newInstance();
  49.                 }
  50.             } catch (ClassNotFoundException e) {
  51.                 System.out.println("Cannot find class " + className + " " + e);
  52.                 result = null;
  53.             } catch (InstantiationException e) {
  54.                 System.out.println("Cannot instantiate class " + className + " " + e);
  55.                 result = null;
  56.             } catch (IllegalAccessException e) {
  57.                 System.out.println("Cannot access class " + className + " " + e);
  58.                 result = null;
  59.             }
  60.             
  61.             if(result == null) {
  62.                 result = new DefaultFocusManager();
  63.             }
  64.             SwingUtilities.appContextPut(focusManagerKey, result);
  65.         }
  66.         return result;
  67.     }
  68.  
  69.     /** Set the FocusManager that should be used for the calling 
  70.      *  thread. <b>aFocusManager</b> will be the default focus
  71.      *  manager for the calling thread's thread group.
  72.      */
  73.     public static void setCurrentManager(FocusManager aFocusManager) {
  74.         if (aFocusManager != null) {
  75.             SwingUtilities.appContextPut(focusManagerKey, aFocusManager);
  76.         } else {
  77.             SwingUtilities.appContextRemove(focusManagerKey);
  78.         }
  79.     }
  80.  
  81.     /** Disable Swing's focus manager for the calling thread's thread group.
  82.      *  Call this method if your application mixes java.awt components and
  83.      *  swing's components. Your application will then use the awt focus 
  84.      *  manager.
  85.      */
  86.     public static void disableSwingFocusManager() {
  87.         setCurrentManager(new DisabledFocusManager());
  88.     }
  89.  
  90.     /** Return whether Swing's focus manager is enabled **/
  91.     public static boolean isFocusManagerEnabled() {
  92.         FocusManager fm = getCurrentManager();
  93.         return !(fm instanceof DisabledFocusManager);
  94.     }
  95.  
  96.     /** This method is called by JComponents when a key event occurs.
  97.      *  JComponent gives key events to the focus manager
  98.      *  first, then to key listeners, then to the keyboard UI dispatcher.
  99.      *  This method should look at the key event and change the focused
  100.      *  component if the key event matches the receiver's focus manager
  101.      *  hot keys. For example the default focus manager will change the
  102.      *  focus if the key event matches TAB or Shift + TAB.
  103.      *  The focus manager should call consume() on <b>anEvent</b> if 
  104.      *  <code>anEvent</code> has been processed. 
  105.      *  <code>focusedComponent</code> is the component that currently has
  106.      *  the focus.
  107.      *  Note: FocusManager will receive both KEY_PRESSED and KEY_RELEASED
  108.      *  key events. If one event is consumed, the other one should be consumed
  109.      *  too.
  110.      */
  111.     public abstract void processKeyEvent(Component focusedComponent,KeyEvent anEvent);
  112.  
  113.  
  114.     /** Cause the focus manager to set the focus on the next focusable component 
  115.      *  You can call this method to cause the focus manager to focus the next component
  116.      **/
  117.     public abstract void focusNextComponent(Component aComponent);
  118.  
  119.     /** Cause the focus manager to set the focus on the previous focusable component 
  120.      *  You can call this methid to cause the focus manager to focus the previous component
  121.      */
  122.     public abstract void focusPreviousComponent(Component aComponent);
  123.  
  124.     static class DisabledFocusManager extends FocusManager {
  125.         public void processKeyEvent(Component focusedComponent,KeyEvent anEvent) {}
  126.         public void focusNextComponent(Component c) {}
  127.         public void focusPreviousComponent(Component c) {}
  128.     }
  129. }
  130.